import { useLocation, history } from '@umijs/max';
import { useEffect } from 'react';



const useWxCode = (fn = async () => { }) => {
    const query = new URLSearchParams(useLocation().search)
    const { pathname } = useLocation();

    useEffect(() => {
        const handleQueryChange = async () => {
            const state = query.get('state');
            const code = query.get('code');
            await fn(state, code, pathname);
        };

        // 监听 location 的变化
        // const unlisten = history.listen(() => {
        //     handleQueryChange();
        // });
        // 初始执行一次
        handleQueryChange();
        // return () => {
        //     unlisten();
        // };
    }, [query]);

    return query;
};

export default useWxCode;